package com.framework.delegate;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.framework.R;
import com.kymjs.frame.presenter.ActivityPresenter;
import com.kymjs.frame.view.AppDelegate;

/**
 * Created by LUOLI on 2017/4/17.
 * <p>
 * 有toolbar的页面继承该类
 */

public abstract class ToolbarDelegate extends AppDelegate {
    private Toolbar mToolbar;
    private View divider;

    @Override
    public void create(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.create(inflater, container, savedInstanceState);
        LinearLayout toolbarRootView = new LinearLayout(getActivity());

        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);//定义布局管理器的参数
        toolbarRootView.setOrientation(LinearLayout.VERTICAL);
        toolbarRootView.setLayoutParams(param);
        mToolbar = (Toolbar) View.inflate(getActivity(), R.layout.base_toolbar, null);
        mToolbar.setTitle("");
        ((ActivityPresenter) getActivity()).setSupportActionBar(mToolbar);

        divider = new View(this.getActivity());
        divider.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
        divider.setBackgroundColor(getActivity().getResources().getColor(R.color.gray_divide));

        toolbarRootView.addView(mToolbar);
        toolbarRootView.addView(divider);
        toolbarRootView.addView(rootView);
        rootView = toolbarRootView;
    }

    @Override
    public void initWidget() {
        super.initWidget();
    }

    @Override
    public Toolbar getToolbar() {
        return mToolbar;
    }

    @Override
    public <T extends View> T bindView(int id) {
        T view = (T) mViews.get(id);
        if (view == null) {
            view = (T) rootView.findViewById(id);
            if (view == null)
                view = (T) getToolbar().findViewById(id);
            mViews.put(id, view);
        }
        return view;
    }

    public void setTitle(String s) {

        TextView textView = get(R.id.toolbar_title);
        textView.setText(s);
    }

    public void setTitleColor(int color) {
        TextView textView = get(R.id.toolbar_title);
        textView.setTextColor(color);
    }
//    public void setCalculateHead(){
//        ((ImageView)get(R.id.toolbar_right_img)).setImageResource(R.drawable.jsq);
//        get(R.id.ll_right).setVisibility(View.VISIBLE);
//        get(R.id.ll_right).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().startActivity(new Intent(getActivity(),CalculateActivity.class));
//            }
//        });
//    }

    //显示返回键
    public void setBackHead() {
        ((TextView) get(R.id.toolbar_left)).setText("");
        ImageView imageView = get(R.id.toolbar_left_iv);
        imageView.setImageResource(R.drawable.back_finish);
        get(R.id.toolbar_left_iv).setVisibility(View.VISIBLE);
        get(R.id.ll_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isFinish = false;
                if (leftImageViewClickListener != null) {
                    // TODO: 2017/9/30 这里处理点击返回键除了关闭页面还需要需要处理的一些事件
                    isFinish = leftImageViewClickListener.OnLeftImageViewClick(view);
                }
                if (!isFinish) {
                    getActivity().finish();
                }
            }
        });
    }

    //显示返回键
    public void setBackHeadGray() {
        ((TextView) get(R.id.toolbar_left)).setText("");
        ImageView imageView = get(R.id.toolbar_left_iv);
        imageView.setImageResource(R.drawable.back_finish_gray);
        get(R.id.toolbar_left_iv).setVisibility(View.VISIBLE);
      get(R.id.ll_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              /*  if (leftImageViewClickListener != null) {
                    // TODO: 2017/9/30 这里处理点击返回键除了关闭页面还需要需要处理的一些事件
                    leftImageViewClickListener.OnLeftImageViewClick(view);
                }
                getActivity().finish();*/
                boolean isFinish = false;
                if (leftImageViewClickListener != null) {
                    // TODO: 2017/9/30 这里处理点击返回键除了关闭页面还需要需要处理的一些事件
                    isFinish = leftImageViewClickListener.OnLeftImageViewClick(view);
                }
                if (!isFinish) {
                    getActivity().finish();
                }
            }
        });
    }

    //替换返回键
    public void setBackHeadFinish() {
        get(R.id.toolbar_left).setVisibility(View.GONE);
        get(R.id.toolbar_left_iv).setVisibility(View.VISIBLE);
        ImageView imageView = get(R.id.toolbar_left_iv);
        imageView.setImageResource(R.drawable.login_finish);
        get(R.id.ll_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (leftImageViewClickListener != null) {
                    // TODO: 2017/9/30 这里处理点击返回键除了关闭页面还需要需要处理的一些事件
                    leftImageViewClickListener.OnLeftImageViewClick(view);
                }
                getActivity().finish();
            }
        });
    }

    // 顶部加搜索框，可直接搜
    public void setShowSearch(String str) {
        get(R.id.toolbar_title).setVisibility(View.GONE);
        View view = get(R.id.fl_toolbar_search);
        view.setVisibility(View.VISIBLE);
        EditText editText = get(R.id.et_search);
        editText.setVisibility(View.VISIBLE);
        editText.setHint(str);
    }

    // 顶部加搜索框，不可直接搜，跳下一个页面
    public void setUnShowSearch(String str) {
        get(R.id.toolbar_title).setVisibility(View.GONE);
        View view = get(R.id.fl_toolbar_search);
        view.setVisibility(View.VISIBLE);
        TextView editText = get(R.id.tv_search);
        editText.setVisibility(View.VISIBLE);
        editText.setHint(str);
    }

    // 隐藏返回键
    public void setHideLeftBack() {

        FrameLayout frameLayout = get(R.id.ll_left);
        frameLayout.setVisibility(View.GONE);
    }

    //显示右侧TextView的显示内容
    public TextView setRightText(String text) {
        TextView textView = get(R.id.toolbar_right);
        textView.setText(text);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRightTextClickListener != null) {
                    onRightTextClickListener.OnRightTextClick(view);
                }
            }
        });
        return textView;
    }

    public TextView getRightText() {
        return get(R.id.toolbar_right);
    }

    /**
     * 设置右侧显示图片
     *
     * @param bgID
     */
    public void setRightBackground(int bgID) {
        ((ImageView) get(R.id.toolbar_img_right)).setImageResource(bgID);
        get(R.id.toolbar_img_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onRightTextClickListener != null) {
                    onRightTextClickListener.OnRightTextClick(view);
                }
            }
        });
    }

    //显示右侧下拉键
    public void setRightHead() {
        ((TextView) get(R.id.toolbar_right)).setText("");

    }

    // 隐藏分割线
    public void setHideDivder() {
        divider.setVisibility(View.GONE);
    }

    // 右侧TextView的点击监听事件
    public void setOnRightTextClickListener(OnRightTextClickListener onRightTextClickListener) {
        this.onRightTextClickListener = onRightTextClickListener;
    }

    public OnRightTextClickListener onRightTextClickListener;

    public interface OnRightTextClickListener {
        void OnRightTextClick(View view);
    }

    public void setLeftImageViewClickListener(OnLeftImageViewClickListener leftImageViewClickListener) {
        this.leftImageViewClickListener = leftImageViewClickListener;
    }

    public OnLeftImageViewClickListener leftImageViewClickListener;

    public interface OnLeftImageViewClickListener {
        boolean OnLeftImageViewClick(View view);
    }
}
